package com.pxxy.springmvc.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/******************
 * @Author yejf
 * @Description TODO
 */
@Configuration
@EnableAspectJAutoProxy //开启AOP
@EnableTransactionManagement //开启申明式事务
@PropertySource("classpath:db.properties") //读取 属性文件
@ImportResource({"classpath:spring/mybatis-spring.xml"})  //读取spring的xml格式 配置文件
public class AppConfigForXml {
    @Autowired
    private Environment env;   //此类来承载 db.properties文件中的属性

    @Bean
    public DataSource dataSource() {
        //使中 druid 连接池
        DruidDataSource dataSource = new DruidDataSource();
        //设置属性
        dataSource.setUrl(env.getProperty("url"));
        dataSource.setDriverClassName(env.getProperty("driver"));
        dataSource.setUsername(env.getProperty("user"));
        dataSource.setPassword(env.getProperty("password"));
        //
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager(@Autowired DataSource dataSource) {
        // 采用 数据源事务管理器[适用于 jdbc和mybatis 技术]
        return new DataSourceTransactionManager(dataSource);
    }
}
